package com.gsafety.wellsfort.service.security.impl;

import com.gsafety.wellsfort.common.context.LoginContextUtil;
import com.gsafety.wellsfort.dao.security.AdminDAO;
import com.gsafety.wellsfort.dao.security.AdminRoleRelDAO;
import com.gsafety.wellsfort.dao.security.MenuDAO;
import com.gsafety.wellsfort.dao.security.MenuRoleRelDAO;
import com.gsafety.wellsfort.domain.model.security.Admin;
import com.gsafety.wellsfort.domain.model.security.AdminRoleRel;
import com.gsafety.wellsfort.domain.model.security.Menu;
import com.gsafety.wellsfort.domain.model.security.MenuRoleRel;
import com.gsafety.wellsfort.domain.vo.MenuVO;
import com.gsafety.wellsfort.service.security.IMenuService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements IMenuService {
    @Autowired
    private MenuDAO menuDAO;
    @Autowired
    private AdminDAO adminDAO;
    @Autowired
    private AdminRoleRelDAO adminRoleRelDAO;
    @Autowired
    private MenuRoleRelDAO menuRoleRelDAO;

    @Override
    public List<Menu> queryAllMenuList() {
        return null;
    }

    @Override
    public List<MenuVO> queryRoleMenuList() {
        List<Menu> menuList = new ArrayList<>();
        List<MenuVO> treeList = new ArrayList<>();
        //获取登录id
        Admin admin = LoginContextUtil.getAdmin();
        List<MenuRoleRel> menuRoleRelList = null;
        List<AdminRoleRel> list = adminRoleRelDAO.selectByAdminId(admin.getId());
        if (list != null && list.size() > 0) {
            for (AdminRoleRel roleRel : list) {
                menuRoleRelList = menuRoleRelDAO.selectByRoleId(roleRel.getRoleId());
                for (MenuRoleRel menuRoleRel : menuRoleRelList) {
                    Menu menu = menuDAO.selectByPrimaryKey(menuRoleRel.getMenuId());
                    menuList.add(menu);
                }
            }
        }
        if (menuList != null) {
            Map<String, MenuVO> cache = new HashMap<>(8);
            MenuVO leaf = null;
            int codeLength = 3;
            if (list != null && list.size() > 0) {
                for (Menu menu : menuList) {
                    //type=3为权限码,不参与菜单返回
                    if (menu.getType() != 3) {
                        leaf = new MenuVO(menu.getId(), menu.getName(), menu.getIcon(), menu.getViewLevelCode());
                        String code = menu.getViewLevelCode();
                        MenuVO parent = null;
                        if (code != null && code.length() > codeLength) {
                            parent = cache.get(StringUtils.substring(code, 0, code.length() - 3));
                        }
                        if (parent != null) {
                            parent.addChildren(leaf);
                        } else {
                            treeList.add(leaf);
                        }
                        cache.put(menu.getViewLevelCode(), leaf);
                    }
                }
            }
            return treeList;
        }
        return null;
    }
}
